package com.show.service.impl;


import com.show.mapper.TraineeMapper;
import com.show.pojo.Trainee;
import com.show.service.TraineeService;
import com.show.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 绛河
 */

public class TraineeServiceImpl implements TraineeService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Trainee> searchAll() {
        SqlSession sqlSession = factory.openSession();
        TraineeMapper mapper = sqlSession.getMapper(TraineeMapper.class);
        List<Trainee> trainees = mapper.searchAll();
        return trainees;
    }

    @Override
    public Integer searchBefore(LocalDateTime localDateTime) {
        SqlSession sqlSession = factory.openSession();
        TraineeMapper mapper = sqlSession.getMapper(TraineeMapper.class);
        Integer integer = mapper.searchBefore(localDateTime);
        return integer;
    }

    @Override
    public Integer searchByTerm(Integer term) {
        SqlSession sqlSession = factory.openSession();
        TraineeMapper mapper = sqlSession.getMapper(TraineeMapper.class);
        Integer integer = mapper.searchByTerm(term);
        return integer;
    }

    @Override
    public List<Map<String, Object>> searchByYear() {
        SqlSession sqlSession = factory.openSession();
        TraineeMapper mapper = sqlSession.getMapper(TraineeMapper.class);
        List<Map<String, Object>> maps = mapper.searchByYear();
        return maps;
    }

    @Override
    public List<Map<String, Object>> searchByMonth(Integer year) {
        SqlSession sqlSession = factory.openSession();
        TraineeMapper mapper = sqlSession.getMapper(TraineeMapper.class);
        List<Map<String, Object>> maps = mapper.searchByMonth(year);
        return maps;
    }

    @Override
    public Integer saveAll(List<Trainee> trainees) {
        SqlSession sqlSession = factory.openSession();
        TraineeMapper mapper = sqlSession.getMapper(TraineeMapper.class);
        Integer integer = mapper.saveAll(trainees);
        sqlSession.commit();
        return integer;
    }

    @Override
    public Integer saveTrainee(Trainee trainee) {
        SqlSession sqlSession = factory.openSession();
        TraineeMapper mapper = sqlSession.getMapper(TraineeMapper.class);
        Integer integer = mapper.saveTrainee(trainee);
        sqlSession.commit();
        return integer;
    }


}
